import platform,os
from base import *


class Nginx(base_tool):

    def CompileInstall(self):
        dist = self.GetDist()
        if dist["ostype"] == "centos":
            self.run("yum install -y wget make gcc gcc-g++ gcc-g77 openssl openssl-devel pcre")
            self.start_compile()
        else:
            self.run("apt-get install wget make gcc gcc-g++ gcc-g77 openssl openssl-dev pcre")


    def start_compile(self):
        self.download("http://nginx.org/download/nginx-" + nginxv + ".tar.gz")
        self.extract_targz("nginx-"+nginxv+".tar.gz")
        self.run("cd nginx-" + nginxv)
        self.run("useradd -M www -s /sbin/nologin")
        self.run("cd nginx-"+nginxv+" && ./configure --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-http_v2_module --user=www --group=www  && make && make install")
        if os.path.exists("/usr/local/nginx/sbin/nginx"):
            print("Install Nginx Success")
            return True
        else:
            print("Install Nginx Failed")
            return False




    def ConfigRepo(self):
        dist = self.GetDist()
        print dist
        if dist["ostype"] == "centos":
            nginxrepo = """[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/%s/%s/$basearch/
gpgcheck=0
enabled=1""" % (dist["ostype"],dist["version"])
            print nginxrepo
            self.writefile(nginxrepo,"/etc/yum.repos.d/nginx.repo")

            r = self.run("yum install -y nginx")
            self.readpopen(r)
            r = self.run("nginx")
            self.readpopen(r)



    def YumInstall(self):
        self.ConfigRepo()

    def APTInstall(self):
        pass




